package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.ConsumMonitor;
import com.dy.nygl.service.ConsumMonitorService;

@Controller
@RequestMapping("/consums")
public class ConsumMonitorController {
	private List<ConsumMonitor> consumList;
	private List<ConsumMonitor> optionList;

	private String sect1;
	private String select1 = "jsp/a.jsp";

	@Resource(name = "consumMonitorService")
	private ConsumMonitorService consumMonitorService;

	@RequestMapping(value = "/insertSelective.action", method = { RequestMethod.POST })
	public ModelAndView insertSelective(HttpServletRequest request, HttpSession session)
			throws ServletException, IOException {
		String various = request.getParameter("various");
		String total = request.getParameter("total");
		String expect = request.getParameter("expect");
		String actual = request.getParameter("actual");

		ConsumMonitor consumMonitor = new ConsumMonitor();
		consumMonitor.setVarious(various.trim());
		consumMonitor.setTotal(total.trim());
		consumMonitor.setExpect(expect.trim());
		consumMonitor.setActual(actual.trim());

		consumMonitorService.insert(consumMonitor);

		ModelAndView model = new ModelAndView();
		model.setViewName("consum_monitor");

		return model;
	}

	@RequestMapping("/updateThis.action")
	public ModelAndView updateThis(@RequestParam("id") Integer id, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		model.addObject("id", id);
		model.setViewName("consums_update");
		return model;
	}

	@RequestMapping("/insertThis.action")
	public ModelAndView insertThis() {
		ModelAndView model = new ModelAndView();

		model.setViewName("consums");
		return model;
	}

	@RequestMapping("/deleteThis.action")
	public void deleteThis(@RequestParam("id") Integer id, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		ModelAndView model = new ModelAndView();
		consumMonitorService.deleteThis(id);
		consumList = consumMonitorService.selectAll();
		optionList = consumMonitorService.selectAll();
		setConsumList(consumList);
		setOptionList(optionList);
		PrintWriter out = response.getWriter();
		out.print(1);
	}

	@RequestMapping(value = "/getSect1.action", method = { RequestMethod.POST })
	public void getSect1(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String sect1 = request.getParameter("sect1");

		setSect1(sect1);
		out.print(3);
	}

	@RequestMapping(value = "/getSelect1.action", method = { RequestMethod.POST })
	public void getSelect1(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String select1 = request.getParameter("select1");

		setSelect1(select1);
		out.print(10);

	}

	@RequestMapping("/searchName.action")
	public void searchName(HttpServletResponse response, HttpSession session) throws ServletException, IOException {

		String sect = getSect1();
		consumList = consumMonitorService.selectByName(sect);
		setConsumList(consumList);

		PrintWriter out = response.getWriter();
		out.print(5);
	}

	public String getSect1() {
		return sect1;
	}

	public void setSect1(String sect1) {
		this.sect1 = sect1;
	}

	public String getSelect1() {
		return select1;
	}

	public void setSelect1(String select1) {
		this.select1 = select1;
	}

	@RequestMapping(value = "/updateSelective.action", method = { RequestMethod.POST })
	public ModelAndView updateSelective(HttpServletRequest request, HttpSession session) throws IOException {
		String id1 = request.getParameter("id");
		int id = Integer.parseInt(id1);
		String various = request.getParameter("various");
		String total = request.getParameter("total");
		String expect = request.getParameter("expect");
		String actual = request.getParameter("actual");

		ConsumMonitor consumMonitor = new ConsumMonitor();
		consumMonitor.setId(id);
		consumMonitor.setVarious(various.trim());
		consumMonitor.setTotal(total.trim());
		consumMonitor.setExpect(expect.trim());
		consumMonitor.setActual(actual.trim());

		consumMonitorService.update(consumMonitor);

		ModelAndView model = new ModelAndView();
		model.setViewName("consum_monitor");

		return model;

	}

	@RequestMapping("/setList.action")
	public void setAllList(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		consumList = consumMonitorService.selectAll();
		setConsumList(consumList);
		setOptionList(consumList);
		PrintWriter out = response.getWriter();
		out.print(1);
	}

	

	@RequestMapping("/getList.action")
	public ModelAndView getList(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		consumList = getConsumList();
		optionList = getOptionList();
		select1 = getSelect1();
		model.addObject("jsppage", select1);
		model.addObject("consumList", consumList);
		model.addObject("optionList", optionList);
		model.setViewName("consum_monitor");
		return model;
	}

	public List<ConsumMonitor> getConsumList() {
		return consumList;
	}

	public void setConsumList(List<ConsumMonitor> consumList) {
		this.consumList = consumList;
	}

	public List<ConsumMonitor> getOptionList() {
		return optionList;
	}

	public void setOptionList(List<ConsumMonitor> optionList) {
		this.optionList = optionList;
	}

}
